/*
 * This file Copyright (c) 2015. Walle.
 * (http://www.wallellen.com). All rights reserved.
 *
 *
 * This file is dual-licensed under both the
 * Walle Agreement (WA) and the GNU General Public License.
 * You may elect to use one or the other of these licenses.
 *
 * This file is distributed in the hope that it will be
 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
 * Redistribution, except as permitted by whichever of the GPL
 * or WA you select, is prohibited.
 *
 * 1. For the GPL license (GPL), you can redistribute and/or
 * modify this file under the terms of the GNU General
 * Public License, Version 3, as published by the Free Software
 * Foundation.  You should have received a copy of the GNU
 * General Public License, Version 3 along with this program;
 * if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * 2. For the Walle Agreement (WA), this file
 * and the accompanying materials are made available under the
 * terms of the WA which accompanies this distribution, and
 * is available at http://www.wallellen.com/agreement.html
 *
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 */

package com.wallellen.knowledge.doc.server.impl;

import com.wallellen.core.auth.domain.LoginUser;
import com.wallellen.core.sql.query.DBRule;
import com.wallellen.core.sql.query.DBSort;
import com.wallellen.core.sql.query.DataQuery;
import com.wallellen.core.sql.result.DataResult;
import com.wallellen.core.time.TimeTool;
import com.wallellen.knowledge.doc.dao.UsermessageDaoInter;
import com.wallellen.knowledge.doc.domain.UserMessage;
import com.wallellen.knowledge.doc.server.UsermessageServiceInter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.sql.SQLException;
import java.util.List;

/* *
 *功能：用户消息服务层实现类
 *详细：
 *
 *版本：v0.1
 *@author <a href="mailto:wallellen@hotmail.com">WALLE</a>
 *日期：20150707114057
 *说明：
 */
@Service
public class UsermessageServiceImpl implements UsermessageServiceInter {
    @Resource
    private UsermessageDaoInter usermessageDaoImpl;

    @Override
    @Transactional
    public UserMessage insertUsermessageEntity(UserMessage entity, LoginUser user) {
        entity.setCtime(TimeTool.getTimeDate12());
        entity.setCuser(user.getId());
        entity.setCusername(user.getName());
        entity.setPstate("1");
        return usermessageDaoImpl.insertEntity(entity);
    }

    @Override
    @Transactional
    public UserMessage editUsermessageEntity(UserMessage entity, LoginUser user) {
        UserMessage entity2 = usermessageDaoImpl.getEntity(entity.getId());
        entity2.setContent(entity.getContent());
        entity2.setReadstate(entity.getReadstate());
        entity2.setReaduserid(entity.getReaduserid());
        entity2.setTitle(entity.getTitle());
        usermessageDaoImpl.editEntity(entity2);
        return entity2;
    }

    @Override
    @Transactional
    public void deleteUsermessageEntity(String id, LoginUser user) {

        usermessageDaoImpl.deleteEntity(usermessageDaoImpl.getEntity(id));
    }

    @Override
    @Transactional
    public UserMessage getUsermessageEntity(String id) {

        if (id == null) {
            return null;
        }
        return usermessageDaoImpl.getEntity(id);
    }

    @Override
    @Transactional
    public DataQuery createUsermessageSimpleQuery(DataQuery query) {

        DataQuery dbQuery = DataQuery.init(query,
                "FARM_USERMESSAGE USERMESSAGE " + "LEFT JOIN ALONE_AUTH_USER USER ON USERMESSAGE.READUSERID = USER.ID",
                "USERMESSAGE.ID AS ID, USERMESSAGE.CTIME AS USERMESSAGECTIME, USERMESSAGE.READUSERID AS READUSERID,USERMESSAGE.CONTENT AS CONTENT,"
                        + "USERMESSAGE.TITLE AS TITLE,USERMESSAGE.READSTATE AS READSTATE,USERMESSAGE.PCONTENT AS PCONTENT,"
                        + "USERMESSAGE.PSTATE AS PSTATE,USERMESSAGE.CUSERNAME AS CUSERNAME,USERMESSAGE.CUSER AS CUSER,"
                        + "USERMESSAGE.CTIME AS CTIME, USER.NAME AS READUSERNAME");
        return dbQuery;
    }

    // ----------------------------------------------------------------------------------
    public UsermessageDaoInter getUsermessageDaoImpl() {
        return usermessageDaoImpl;
    }

    public void setUsermessageDaoImpl(UsermessageDaoInter dao) {
        this.usermessageDaoImpl = dao;
    }

    @Override
    @Transactional
    public void setRead(String id) {
        UserMessage entity = usermessageDaoImpl.getEntity(id);
        entity.setReadstate("1");
    }

    @Override
    @Transactional
    public void sendMessage(String readUserId, String text, String title, String note, LoginUser sendUser) {
        UserMessage message = new UserMessage();
        message.setContent(text);
        message.setCtime(TimeTool.getTimeDate14());
        if (sendUser != null) {
            message.setCuser(sendUser.getId());
            message.setCusername(sendUser.getName());
        } else {
            message.setCuser("SYS");
            message.setCusername("系统消息");
        }
        if (note != null) {
            message.setPcontent(note);
        }
        message.setPstate("1");
        message.setReadstate("0");
        message.setReaduserid(readUserId);
        message.setTitle(title);
        usermessageDaoImpl.insertEntity(message);
    }

    @Override
    @Transactional
    public void sendMessage(String readUserId, String text, String title, LoginUser sendUser) {
        sendMessage(readUserId, text, title, null, sendUser);
    }

    @Override
    @Transactional
    public void sendMessage(String readUserId, String text, String title) {
        sendMessage(readUserId, text, title, null, null);
    }

    @Override
    @Transactional
    public void sendMessage(List<String> userids, String text, String title) {
        for (String id : userids) {
            sendMessage(id, text, title);
        }
    }

    @Override
    public DataResult getMyMessageByUser(String userId, int pageSize, Integer currPage) {
        DataQuery query = createUsermessageSimpleQuery(null);
        query.addRule(new DBRule("USERMESSAGE.READUSERID", userId, "="));
        query.addSort(new DBSort("USERMESSAGE.CTIME", "DESC"));
        query.setCurrentPage(currPage);
        query.setPagesize(pageSize);
        try {
            return query.search();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
